using System;
using System.IO;
using EPiServer.Common;
using EPiServer.Common.Security;
using EPiServer.Community.Webmail;

namespace EPiAbstractions.Community.Webmail
{
    public class WebmailHandlerFacade : IWebmailHandlerFacade
    {
        private static WebmailHandlerFacade _instance;

        public WebmailHandlerFacade()
        {
            WebmailHandler.MailAccountAdded += OnMailAccountAdded;
            WebmailHandler.MailAccountRemoved += OnMailAccountRemoved;
            WebmailHandler.MailboxAdded += OnMailboxAdded;
            WebmailHandler.MailboxRemoved += OnMailboxRemoved;
            WebmailHandler.MessageAdded += OnMessageAdded;
            WebmailHandler.MessageSent += OnMessageSent;
            WebmailHandler.MessageRemoved += OnMessageRemoved;
        }

        public static WebmailHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new WebmailHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IWebmailHandlerFacade Members

        public event EPiServerCommonEventHandler MailAccountAdded;
        public event EPiServerCommonEventHandler MailAccountRemoved;
        public event EPiServerCommonEventHandler MailboxAdded;
        public event EPiServerCommonEventHandler MailboxRemoved;
        public event EPiServerCommonEventHandler MessageAdded;
        public event EPiServerCommonEventHandler MessageSent;
        public event EPiServerCommonEventHandler MessageRemoved;

        public virtual MailAccount AddMailAccount(MailAccount mailAccount)
        {
            return WebmailHandler.AddMailAccount(mailAccount);
        }

        public virtual void UpdateMailAccount(MailAccount mailAccount)
        {
            WebmailHandler.UpdateMailAccount(mailAccount);
        }

        public virtual void RemoveMailAccount(MailAccount mailAccount)
        {
            WebmailHandler.RemoveMailAccount(mailAccount);
        }

        public virtual MailAccount GetMailAccount(Int32 id)
        {
            return WebmailHandler.GetMailAccount(id);
        }

        public virtual MailAccountCollection GetMailAccounts(IUser user)
        {
            return WebmailHandler.GetMailAccounts(user);
        }

        public virtual MailAccountCollection GetMailAccounts(IUser user, MailServer mailServer)
        {
            return WebmailHandler.GetMailAccounts(user, mailServer);
        }

        public virtual MailServer AddMailServer(MailServer mailServer)
        {
            return WebmailHandler.AddMailServer(mailServer);
        }

        public virtual void UpdateMailServer(MailServer mailServer)
        {
            WebmailHandler.UpdateMailServer(mailServer);
        }

        public virtual void RemoveMailServer(MailServer mailServer)
        {
            WebmailHandler.RemoveMailServer(mailServer);
        }

        public virtual MailServer GetMailServer(Int32 id)
        {
            return WebmailHandler.GetMailServer(id);
        }

        public virtual MailServer GetMailServer(String name)
        {
            return WebmailHandler.GetMailServer(name);
        }

        public virtual MailServerCollection GetMailServers()
        {
            return WebmailHandler.GetMailServers();
        }

        public virtual void SoftRemoveMailAccount(InternalMailAccount mailAccount)
        {
            WebmailHandler.SoftRemoveMailAccount(mailAccount);
        }

        public virtual void SoftRestoreMailAccount(InternalMailAccount mailAccount)
        {
            WebmailHandler.SoftRestoreMailAccount(mailAccount);
        }

        public virtual void SetQuota(InternalMailAccount mailAccount, Int32 quota)
        {
            WebmailHandler.SetQuota(mailAccount, quota);
        }

        public virtual void UpdatePassword(InternalMailAccount mailAccount)
        {
            WebmailHandler.UpdatePassword(mailAccount);
        }

        public virtual Mailbox GetRootMailbox(MailAccount mailAccount)
        {
            return WebmailHandler.GetRootMailbox(mailAccount);
        }

        public virtual String GetHTMLBody(ServerMessage message)
        {
            return WebmailHandler.GetHTMLBody(message);
        }

        public virtual String GetTextBody(ServerMessage message)
        {
            return WebmailHandler.GetTextBody(message);
        }

        public virtual AttachmentCollection GetAttachments(ServerMessage message)
        {
            return WebmailHandler.GetAttachments(message);
        }

        public virtual Stream GetAttachmentStream(ServerAttachment attachment)
        {
            return WebmailHandler.GetAttachmentStream(attachment);
        }

        public virtual MailAccountStatus GetMailAccountStatus(InternalMailAccount mailAccount)
        {
            return WebmailHandler.GetMailAccountStatus(mailAccount);
        }

        public virtual MailAccountStatus GetMailAccountStatus(IUser user, InternalMailServer mailServer)
        {
            return WebmailHandler.GetMailAccountStatus(user, mailServer);
        }

        public virtual InternalMailAccount GetInternalMailAccount(IUser user, InternalMailServer mailServer)
        {
            return WebmailHandler.GetInternalMailAccount(user, mailServer);
        }

        public virtual Mailbox AddMailbox(String name, Mailbox parent)
        {
            return WebmailHandler.AddMailbox(name, parent);
        }

        public virtual void RemoveMailbox(Mailbox mailbox)
        {
            WebmailHandler.RemoveMailbox(mailbox);
        }

        public virtual ServerMessage GetMessage(UInt32 messageId, Mailbox mailbox)
        {
            return WebmailHandler.GetMessage(messageId, mailbox);
        }

        public virtual MessageCollection GetMessages(Mailbox mailbox, Int32 page, Int32 pageSize, MessageSortOrder sortOrder)
        {
            return WebmailHandler.GetMessages(mailbox, page, pageSize, sortOrder);
        }

        public virtual MessageCollection GetMessages(Mailbox mailbox, Int32 page, Int32 messagesPerPage, out Int32 totalItems,
                                                     MessageSortOrder sortOrder)
        {
            return WebmailHandler.GetMessages(mailbox, page, messagesPerPage, out totalItems, sortOrder);
        }

        public virtual void MoveMessage(ServerMessage message, Mailbox destination)
        {
            WebmailHandler.MoveMessage(message, destination);
        }

        public virtual void RemoveMessage(ServerMessage message)
        {
            WebmailHandler.RemoveMessage(message);
        }

        public virtual MessageCollection Search(String needle, Mailbox haystack, Int32 page, Int32 pageSize, MessageSortOrder sortOrder)
        {
            return WebmailHandler.Search(needle, haystack, page, pageSize, sortOrder);
        }

        public virtual MessageCollection Search(String needle, Mailbox haystack, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                MessageSortOrder sortOrder)
        {
            return WebmailHandler.Search(needle, haystack, page, pageSize, out totalItems, sortOrder);
        }

        public virtual void AddMessage(Mailbox mailbox, LocalMessage message)
        {
            WebmailHandler.AddMessage(mailbox, message);
        }

        public virtual void SendMessage(LocalMessage message, MailAccount mailAccount)
        {
            WebmailHandler.SendMessage(message, mailAccount);
        }

        public virtual void SendMessage(LocalMessage message, Boolean removeAttachedFiles, MailAccount mailAccount)
        {
            WebmailHandler.SendMessage(message, removeAttachedFiles, mailAccount);
        }

        public virtual QuotaStats GetQuotaStats(MailAccount mailAccount)
        {
            return WebmailHandler.GetQuotaStats(mailAccount);
        }

        public virtual void SendMessage(LocalMessage message, Mailbox mailbox)
        {
            WebmailHandler.SendMessage(message, mailbox);
        }

        public virtual void SendMessage(LocalMessage message, Mailbox mailbox, Boolean removeAttachedFiles)
        {
            WebmailHandler.SendMessage(message, mailbox, removeAttachedFiles);
        }

        public virtual void UpdateMailAccountDomain(InternalMailAccount mailAccount)
        {
            WebmailHandler.UpdateMailAccountDomain(mailAccount);
        }

        public virtual Domain AddDomain(Domain domain)
        {
            return WebmailHandler.AddDomain(domain);
        }

        public virtual DomainCollection GetDomains()
        {
            return WebmailHandler.GetDomains();
        }

        public virtual Domain GetDomain(Int32 id)
        {
            return WebmailHandler.GetDomain(id);
        }

        public virtual Domain GetDomainByDomainName(String domainName)
        {
            return WebmailHandler.GetDomainByDomainName(domainName);
        }

        public virtual Domain GetDomainByMailAccount(InternalMailAccount mailAccount)
        {
            return WebmailHandler.GetDomainByMailAccount(mailAccount);
        }

        public virtual void UpdateDomain(Domain domain)
        {
            WebmailHandler.UpdateDomain(domain);
        }

        public virtual void RemoveDomain(Domain domain)
        {
            WebmailHandler.RemoveDomain(domain);
        }

        public virtual void UpdateServerMessage(ServerMessage message)
        {
            WebmailHandler.UpdateServerMessage(message);
        }

        #endregion

        public virtual void OnMailAccountAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (MailAccountAdded != null)
                MailAccountAdded(sender, e);
        }

        public virtual void OnMailAccountRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (MailAccountRemoved != null)
                MailAccountRemoved(sender, e);
        }

        public virtual void OnMailboxAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (MailboxAdded != null)
                MailboxAdded(sender, e);
        }

        public virtual void OnMailboxRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (MailboxRemoved != null)
                MailboxRemoved(sender, e);
        }

        public virtual void OnMessageAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (MessageAdded != null)
                MessageAdded(sender, e);
        }

        public virtual void OnMessageSent(String sender, EPiServerCommonEventArgs e)
        {
            if (MessageSent != null)
                MessageSent(sender, e);
        }

        public virtual void OnMessageRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (MessageRemoved != null)
                MessageRemoved(sender, e);
        }
    }
}